﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using CCF20Model;
using System.Windows;
using System.Windows.Media;
using WiseLinkWPF.Windows.Controls;
using WiseLink.RDS.Resource;
using CCF20.Shared;
using System.Threading;


namespace CCF20Module
{
    public partial class CCFBase : UserControl
    {
        protected ModelFactory modelFactory;
        protected DeviceModel deviceModel;
        protected VehicleModel vehicleModel;
        protected CustomerModel customerModel;
        protected CustomizeModel customizeModel;
        protected FaultModel faultModel;
        protected VehicleMaintenanceModel vrmModel;

        public CCFBase()
        {
            modelFactory = new ModelFactory();
            faultModel = (FaultModel)modelFactory.CreateModel(ModelName.FaultModel);
            deviceModel = (DeviceModel)modelFactory.CreateModel(ModelName.DeviceModel);
            vehicleModel = (VehicleModel)modelFactory.CreateModel(ModelName.VehicleModel);
            customerModel = (CustomerModel)modelFactory.CreateModel(ModelName.CustomerModel);
            customizeModel = (CustomizeModel)modelFactory.CreateModel(ModelName.CustomizeModel);
            vrmModel = (VehicleMaintenanceModel)modelFactory.CreateModel(ModelName.VehicleMaintenanceModel);
            
        }

        
        public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject 
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) 
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T) 
                    {
                        yield return (T)child; 
                    }
                    foreach (T childOfChild in FindVisualChildren<T>(child))
                    {
                        yield return childOfChild; 
                    }
                }
            }
        }

        public void ApplyResourceToButton()
        {
            foreach (Button btn in FindVisualChildren<Button>(this))
            {
                btn.Content = WiseLink.RDS.Resource.ButtonString.ResourceManager.GetString(btn.Name)?? btn.Content;
            }
        }
        public void ApplyResourceToCLRObjectBoundTextBox()
        {
            foreach (CLRObjectBoundTextBox btb in FindVisualChildren<CLRObjectBoundTextBox>(this))
            {
                string columnName = btb.BoundPropertyName.Substring(btb.BoundPropertyName.LastIndexOf('.') + 1);
                btb.LabelToDisplay = WiseLink.RDS.Resource.CustomerTableString.ResourceManager.GetString(columnName) ?? btb.LabelToDisplay;
            }
        }
        public void ApplyResourceToLable()
        {
            foreach (Label lbl in FindVisualChildren<Label>(this))
            {
                lbl.Content = WiseLink.RDS.Resource.CustomerTableString.ResourceManager.GetString(lbl.Name) ?? 
                              (WiseLink.RDS.Resource.ButtonString.ResourceManager.GetString(lbl.Name)??lbl.Content);
            }
        }
        public void ApplyResourceToGrid()
        {
            foreach (RadGridView radGridView in FindVisualChildren<RadGridView>(this))
            {

                ApplyResourceToColumn(radGridView);
               
            }
          
        }
        public void ApplyResourceToTabTitle(TabControl tabControl)
        {
            foreach (TabItem tabItem in tabControl.Items)
            {
                foreach (TextBlock tbx in FindVisualChildren<TextBlock>(tabItem).Where(t => !string.IsNullOrEmpty(t.Name)))
                {
                    tbx.Text = WiseLink.RDS.Resource.ButtonString.ResourceManager.GetString(tbx.Name) ?? tbx.Text;
                }
            }
            
        }

        public void ApplyResourceToColumn(RadGridView radGridView)
        {
            radGridView.Columns.Where<WiseLinkWPF.Windows.Controls.GridViewColumn>(c => c is WiseLinkWPF.Windows.Controls.GridViewDataColumn)
                                                     .ToList<WiseLinkWPF.Windows.Controls.GridViewColumn>().ForEach
                                                     (
                                                          x =>
                                                          {
                                                              WiseLinkWPF.Windows.Controls.GridViewDataColumn column = x as WiseLinkWPF.Windows.Controls.GridViewDataColumn;
                                                              if (column != null)
                                                              {
                                                                  if (column.UniqueName != null && CustomerTableString.ResourceManager.GetObject(column.UniqueName.GetColumnName()) != null)
                                                                  {
                                                                      x.Header = WiseLink.RDS.Resource.CustomerTableString.ResourceManager.GetObject(column.UniqueName.GetColumnName());
                                                                  }
                                                                  else if (column.Name != null && CustomerTableString.ResourceManager.GetObject(column.Name.GetColumnName()) != null)
                                                                  {
                                                                      x.Header = WiseLink.RDS.Resource.CustomerTableString.ResourceManager.GetObject(column.Name.GetColumnName());
                                                                  }
                                                                  else if (column.DataMemberBinding != null && CustomerTableString.ResourceManager.GetObject(column.DataMemberBinding.Path.Path.GetColumnName()) != null)
                                                                  {
                                                                      x.Header = WiseLink.RDS.Resource.CustomerTableString.ResourceManager.GetObject(column.DataMemberBinding.Path.Path.GetColumnName());
                                                                  }
                                                              }

                                                          });

        }
    }

}
